package edu.ou.herbarium.threads;
import java.net.*;
import java.io.*;

public class MultiServerThread extends Thread {
	private Socket connectionSocket = null;

	public MultiServerThread(Socket socket) {
		super("MultiServerThread");
		this.connectionSocket = socket;
	}

	public void run() {

		try {
			// // PrintWriter out = new PrintWriter(socket.getOutputStream(),
			// true);
			// BufferedReader in = new BufferedReader(
			// new InputStreamReader(
			// socket.getInputStream()));
			// DataOutputStream out = new
			// DataOutputStream(socket.getOutputStream());
			// String inputLine, outputLine;
			//
			// while ((inputLine = in.readLine()) != null) {
			// outputLine = inputLine.toUpperCase();
			// System.out.println("received:"+inputLine);
			// out.writeBytes(outputLine);
			// if (outputLine.equals("BYE"))
			// break;
			// }
			String clientSentence, capitalizedSentence;
			BufferedReader inFromClient = new BufferedReader(
					new InputStreamReader(connectionSocket.getInputStream()));
			DataOutputStream outToClient = new DataOutputStream(
					connectionSocket.getOutputStream());
			while ((clientSentence = inFromClient.readLine()) != null) {
				System.out.println("Received: " + clientSentence);
				capitalizedSentence = clientSentence.toUpperCase() + '\n';
				outToClient.writeBytes(capitalizedSentence);
				if (capitalizedSentence.equals("BYE"))
					break;
			}
			outToClient.close();
			inFromClient.close();
			connectionSocket.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}